﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FksAutoTask
{
    public partial class frm_task : App_Code.WinClient
    {
        public frm_task()
        {
            InitializeComponent();
        }

        private void link_template_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string error = string.Empty;
            string _source_file = Application.StartupPath + "\\import.dll";
            string _target_file = Application.StartupPath + "\\tmp\\" + Fks.Common.RndUntity.GetSystemID23() + ".xlsx";
            if (Fks.Common.TextIoUntity.Copy(_source_file, _target_file, out error))
                Fks.Common.Win.ProcessUntity.Run_Program(_target_file);
        }

        private void btn_SelectFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            this.tb_File.Text = this.openFileDialog1.FileName;

            string Sheets = Fks.Common.Lib.NOPIUntity.get_Sheets(this.tb_File.Text, Fks.Common.EnumUntity.NopiTrager.Office2007);
            for (int i = 0; i < Sheets.Split(',').Length; i++)
            {
                if (i == 0)
                    this.cb_sheet.Text = Sheets.Split(',')[0];

                this.cb_sheet.Items.Add(Sheets.Split(',')[i]);
            }

            this.btn_Save.Enabled = true;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.tb_File.Text.Equals(""))
                MessageBox.Show("请选择需要上传的文件！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                this.btn_Save.Enabled = false;

                string error = string.Empty;
                DataTable myda = Fks.Common.Lib.NOPIUntity.Import(this.tb_File.Text, this.cb_sheet.SelectedText, out error);
                if (myda != null && myda.Rows.Count > 0)
                {
                    Fks.DataBase.SqlObject sqlobject = new Fks.DataBase.SqlObject();
                    Hashtable sql = new Hashtable();

                    string _sql_task = "insert into task (id,group_id,month,email,name,status) values ('{0}','{1}','{2}','{3}','{4}','{5}')";
                    string _sql_task_list = "insert into task_list (id,serial,table_name,table_value) values ('{0}','{1}','{2}','{3}')";

                    //分组序号	月份	邮箱地址	姓名	基本工资	考核工资	社保	公积金
                    for (int i = 0; i < myda.Rows.Count; i++)
                    {
                        DataRow dr = myda.Rows[i];
                        string _id = Fks.Common.RndUntity.GetSystemID23();
                        int _serial = 0;

                        #region 获得主任务

                        string _group_id = dr[0].ToString();
                        string _month = dr[1].ToString();
                        string _email = dr[2].ToString();
                        string _name = dr[3].ToString();

                        sql.Add("1_task_" + i.ToString(), string.Format(_sql_task, _id, _group_id, _month, _email, _name, 0));

                        #endregion

                        #region 获得明细的值

                        for (int m = 4; m < myda.Columns.Count; m++)
                        {
                            string _col_name = myda.Columns[m].ColumnName;
                            sql.Add("2_task_list_" + i.ToString() + "_" + m.ToString(), string.Format(_sql_task_list, _id, _serial, _col_name, dr[_col_name].ToString()));
                            _serial++;
                        }

                        #endregion
                    }

                    if (sqlobject.Commit(sql))
                    {
                        var result = MessageBox.Show("成功上传[" + myda.Rows.Count.ToString() + "]条记录！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (result == System.Windows.Forms.DialogResult.OK)
                        {
                            //刷新父窗口的列表
                            frm_main fm = (frm_main)this.Owner;
                            fm.Shows();

                            this.Hide();
                        }
                    }
                    else
                        MessageBox.Show("上传发生系统错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("附件无法识别！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.btn_Save.Enabled = true;
            }
        }

        private void frm_att_task_Load(object sender, EventArgs e)
        {
            Init();
        }
    }
}
